package com.example.administrator.capital.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.example.administrator.capital.R;
import com.example.administrator.capital.adapter.callback.OnPopWinDisMisBack;

/**
 * Created by Zhouziwen .
 * 邮箱：1916973618@qq.com
 */
public abstract class CustomPopupWindow implements PopupWindow.OnDismissListener {
    private PopupWindow mPopupWindow;
    private Window mWindow;
    private OnPopWinDisMisBack mOnPopWinDisMisBack;
    public boolean outSideWindow;
    public CustomPopupWindow(Context context, int LayoutId, int width, int height, OnPopWinDisMisBack onPopWinDisMisBack) {
        initView(context, LayoutId, width, height);
        mOnPopWinDisMisBack = onPopWinDisMisBack;
    }
    public CustomPopupWindow(Window window, Context context, int LayoutId, int width, int height, OnPopWinDisMisBack onPopWinDisMisBack) {
        initView(context, LayoutId, width, height);
        mWindow = window;
        mOnPopWinDisMisBack = onPopWinDisMisBack;
    }
    public CustomPopupWindow(View view, Context context, int LayoutId, int width, int height, OnPopWinDisMisBack onPopWinDisMisBack) {
        init(context,LayoutId,width,height);
        mOnPopWinDisMisBack = onPopWinDisMisBack;
    }
    private void initView(Context context, int LayoutId, int width, int height) {
            mPopupWindow = new PopupWindow(context);
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View BgView = inflater.inflate(LayoutId, null);

            //为了去掉popWin周围默认黑色的边框
            Drawable drawable = new ColorDrawable(context.getResources().getColor(R.color.black_overlay));
            mPopupWindow.setBackgroundDrawable(drawable);
            mPopupWindow.setContentView(BgView);
            mPopupWindow.setFocusable(true);
            mPopupWindow.setWidth(width);
            mPopupWindow.setHeight(height);
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setOnDismissListener(this);
            //这个方法传了一个view布局 可以在view上找到对应的控件 并赋值
            setData(BgView);


    }

    private void init(Context context, int LayoutId, int width, int height) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(LayoutId, null);
        mPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,  LinearLayout.LayoutParams.MATCH_PARENT, true);
        //为了去掉popWin周围默认黑色的边框
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setFocusable(true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setOnDismissListener(this);
        mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });
        view.setFocusableInTouchMode(true);
        setData(view);
    }

    //在弹出popWindow时给背景window添加一个背景
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mWindow.setAttributes(lp);
    }
    public abstract void setData(View view);
    //判断popWindow是否正在显示
    public boolean isShow() {
        return mPopupWindow.isShowing();
    }
    //*******************************控制popWindow的显示于隐藏**************************//

    //将popWindow显示到指定view控件下方
    public void showAsParentView(View view) {

        if (mWindow!=null){
            setBackgroundAlpha(0.6f);
        }
        if (view != null) {
            if (Build.VERSION.SDK_INT >= 24) {
                int[] location = new int[2];
                view.getLocationOnScreen(location);
                int x = location[0];
                int y = location[1];
                // 7.1 版本处理
                if (Build.VERSION.SDK_INT == 25) {
                    //【note!】Gets the screen height without the virtual key
                    WindowManager wm = (WindowManager) mPopupWindow.getContentView().getContext().getSystemService(Context.WINDOW_SERVICE);
                    int screenHeight = wm.getDefaultDisplay().getHeight();
                    mPopupWindow.setHeight(screenHeight - location[1] - view.getHeight() - 0);
                }
                mPopupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, location[1] + view.getHeight() + 0);
            } else {
                mPopupWindow.showAsDropDown(view, 0, 0);
            }
            mPopupWindow.showAsDropDown(view);
        }
    }
    //将popWindow从屏幕下方弹出来
    public void showAsDownWindow(View parentView) {
//        if (outSideWindow) {
//
//        }
        //默认从底部弹出来的popWindow 加了动画
        //该动画是popWindow从底部弹出时需要加的动画
        mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        mPopupWindow.showAtLocation(parentView,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        if (mWindow != null) {
            setBackgroundAlpha(0.7f);
        }
    }
    //将popWindow显示到指定的x y坐标   parentView为popWin的背景view
    public void showAsXYPoint(View parentView, int x, int y) {
        //    mPopupWindow.setAnimationStyle(R.style.AnimationLeftFade);
        mPopupWindow.showAsDropDown(parentView, x, y,Gravity.RIGHT);
    }
    public void showAtLocation(View parentView, int x, int y) {
        //    mPopupWindow.setAnimationStyle(R.style.AnimationLeftFade);
        mPopupWindow.showAtLocation(parentView, Gravity.CENTER,x,y);
    }
    //让popWindow dismiss
    public void dismissWindow() {
        mPopupWindow.dismiss();
    }
    @Override
    public void onDismiss() {
        if (mWindow!=null) {
            setBackgroundAlpha(1f);
        }
        mOnPopWinDisMisBack.onPopWindowDismiss();
    }
}
